package com.hystrix.controller;

import com.common.pojo.Dept;
import com.hystrix.service.DeptHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.print.DocFlavor;

/**
 * @Author Str
 * @Date 2022/1/17 17:08
 * @Description :
 */
@RestController
@RequestMapping("/deptHystrix")
public class DeptHystrixController {
    @Autowired
    private DeptHystrixService deptHystrixService;

    // 获取一条信息
    // http://localhost:9501/deptHystrix/getDeptById?id=1
    @GetMapping("/getDeptById")
    String getDeptById(@RequestParam("id") Integer id) {
        Dept dept = deptHystrixService.getDeptById(id);
        if (dept == null) {
            return "消息接口繁忙，请稍后重试！";
        }
        return dept + "<br/>" + "DeptHystrixController.getDeptById";
    }

    // 获取长等待信息
    // http://localhost:9501/deptHystrix/getLongTime
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "10000")
//    })
//    @HystrixCommand(
//            fallbackMethod = "fallbackTimeout",
//            commandProperties = {
//                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000")
//    })  // 优先级更高
    @GetMapping("/getLongTime")
    String getLongTime() {
        return deptHystrixService.getLongTime();
    }

    // 获取短等待信息
    // http://localhost:9501/deptHystrix/getShortTime
    @GetMapping("/getShortTime")
    String getShortTime() {
        return deptHystrixService.getShortTime();
    }

    public String fallbackTimeout() {
        return "连接超时";
    }

    // 当num为0时发生错误以便测试
    // http://localhost:9501/deptHystrix/divide?num=0
    @GetMapping("/divide")
    public String divide(@RequestParam("num") Integer num) {
        return deptHystrixService.divide(num);
    }
}
